# -*- coding: utf-8 -*-
'''
Created on 05.06.2011

@author: mutius
'''

import wx
from GlobalVars import global_types
from wx._core import EVT_CHECKBOX, EVT_BUTTON, EVT_COMBOBOX

class AddColumnDialog(wx.Dialog):
    def __init__(self, parent, id, cursor):
        wx.Dialog.__init__(self, parent, id)
        self.cursor = cursor
        self.returnedData = dict()
        self.sizer_18_staticbox = wx.StaticBox(self, -1, u"опции")
        self.label_5 = wx.StaticText(self, -1, u"название")
        self.label_6 = wx.StaticText(self, -1, u"тип данных")
        self.text_ctrl_4 = wx.TextCtrl(self, -1, "")
        self.combo_box_2 = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN|wx.CB_READONLY)
        self.checkbox_3 = wx.CheckBox(self, -1, u"значение по умолчанию")
        self.text_ctrl_5 = wx.TextCtrl(self, -1, "")
        self.checkbox_4 = wx.CheckBox(self, -1, u"первичный ключ")
        self.checkbox_6 = wx.CheckBox(self, -1, u"автоинкремент")
        self.checkbox_5 = wx.CheckBox(self, -1, u"внешний")
        self.label_10 = wx.StaticText(self, -1, u"из таблицы ")
        self.combo_box_4 = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN|wx.CB_READONLY)
        self.label_11 = wx.StaticText(self, -1, u"по ссылке на")
        self.combo_box_5 = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN|wx.CB_READONLY)
        self.button_6 = wx.Button(self, -1, u"Ок")

        self.SetTitle(u"добавление поля")
        self.text_ctrl_5.Enable(False)
        self.checkbox_6.Enable(False)
        self.combo_box_2.SetItems(global_types)
        
        tables = self.cursor.execGetTables()
        self.combo_box_4.SetItems(tables)
        self.text_ctrl_5.Enable(False)
        self.combo_box_4.Enable(False)
        self.combo_box_5.Enable(False)
        
        sizer_14 = wx.BoxSizer(wx.VERTICAL)
        sizer_19 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_18 = wx.StaticBoxSizer(self.sizer_18_staticbox, wx.VERTICAL)
        sizer_20 = wx.BoxSizer(wx.VERTICAL)
        sizer_22 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_21 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_17 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_16 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_15 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_15.Add(self.label_5, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_BOTTOM, 20)
        sizer_15.Add(self.label_6, 1, wx.LEFT|wx.ALIGN_BOTTOM, 2)
        sizer_14.Add(sizer_15, 1, wx.EXPAND, 0)
        sizer_16.Add(self.text_ctrl_4, 1, wx.LEFT, 20)
        sizer_16.Add(self.combo_box_2, 1, wx.RIGHT, 20)
        sizer_14.Add(sizer_16, 1, wx.EXPAND, 0)
        sizer_17.Add(self.checkbox_3, 1, wx.LEFT, 20)
        sizer_17.Add(self.text_ctrl_5, 1, wx.RIGHT, 20)
        sizer_14.Add(sizer_17, 1, wx.EXPAND, 0)
        sizer_18.Add(self.checkbox_4, 0, 0, 0)
        sizer_18.Add(self.checkbox_6, 0, 0, 0)
        sizer_18.Add(self.checkbox_5, 0, wx.RIGHT, 20)
        sizer_21.Add(self.label_10, 0, 0, 0)
        sizer_21.Add(self.combo_box_4, 0, 0, 0)
        sizer_20.Add(sizer_21, 1, wx.ALIGN_RIGHT, 0)
        sizer_22.Add(self.label_11, 0, wx.RIGHT, 13)
        sizer_22.Add(self.combo_box_5, 0, 0, 0)
        sizer_20.Add(sizer_22, 1, wx.ALIGN_RIGHT, 0)
        sizer_18.Add(sizer_20, 1, wx.EXPAND, 0)
        sizer_14.Add(sizer_18, 2, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10)
        sizer_19.Add(self.button_6, 0, wx.ALIGN_RIGHT, 0)
        sizer_14.Add(sizer_19, 1, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 20)
        self.SetSizer(sizer_14)
        sizer_14.Fit(self)
        self.Layout()
        
        self.Bind(EVT_CHECKBOX, self.onDefaultFlagChange, self.checkbox_3)
        self.Bind(EVT_CHECKBOX, self.onForeignFlagChange, self.checkbox_5)
        self.Bind(EVT_COMBOBOX, self.onComboboxChanged, self.combo_box_2)
        self.Bind(EVT_COMBOBOX, self.onForeignTableChecked, self.combo_box_4)
        self.Bind(EVT_BUTTON, self.onOk, self.button_6)
        
    def onDefaultFlagChange(self, event):
        self.text_ctrl_5.Enable(event.Checked())
    
    def onForeignFlagChange(self, event):
        self.combo_box_4.Enable(event.Checked())
        self.combo_box_5.Enable(event.Checked())
    
    def onComboboxChanged(self, event):
        self.checkbox_6.Enable(event.String == u"int")
    
    def onForeignTableChecked(self, event):
        columns = self.cursor.getColumnLabels(self.combo_box_4.Value)
        self.combo_box_5.SetItems(columns)
    
    def onOk(self, event):
        name = self.text_ctrl_4.GetValue()
        if len(name) == 0:
            message = wx.MessageDialog(None, u"некорректное название", u"Message", wx.ICON_HAND)
            message.ShowModal()
            message.Destroy()
        else:
            self.returnedData["name"] = name
            self.returnedData["type"] = self.combo_box_2.Value
            self.returnedData["default"] = None
            if self.checkbox_3.Value == True and len(self.text_ctrl_5.GetValue()) != 0:
                self.returnedData["default"] = self.text_ctrl_5.GetValue()
            self.returnedData["primary"] = self.checkbox_4.Value
            self.returnedData["foreign"] = self.checkbox_5.Value
            if self.returnedData["foreign"] == True:
                self.returnedData["references_table"] = self.combo_box_4.Value
                self.returnedData["references_column"] = self.combo_box_5.Value
            self.returnedData["autoincrement"] = self.checkbox_6.Value
            self.Close()